package com.cby.android.olive.ext

import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.*

/**
 * @description: ResourceExt
 *
 * @author ChenBaoYang
 * @date 6/4/21 16:58
 */

private const val RES_ID = "id"
private const val RES_STRING = "string"
private const val RES_DRAWABLE = "drawable"
private const val RES_LAYOUT = "layout"
private const val RES_STYLE = "style"
private const val RES_COLOR = "color"
private const val RES_DIMEN = "dimen"
private const val RES_ANIM = "anim"
private const val RES_MENU = "menu"

fun Fragment.getId(resName: String?): Int {
    return requireActivity().getId(resName)
}

fun Fragment.getStringId(resName: String?): Int {
    return requireActivity().getStringId(resName)
}

fun Fragment.getDrawableId(resName: String?): Int {
    return requireActivity().getDrawableId(resName)
}

fun Fragment.getLayoutId(resName: String?): Int {
    return requireActivity().getLayoutId(resName)
}

fun Fragment.getStyleId(resName: String?): Int {
    return requireActivity().getStyleId(resName)
}

fun Fragment.getColorId(resName: String?): Int {
    return requireActivity().getColorId(resName)
}

fun Fragment.getDimenId(resName: String?): Int {
    return requireActivity().getDimenId(resName)
}

fun Fragment.getAnimId(resName: String?): Int {
    return requireActivity().getAnimId(resName)
}

fun Fragment.getMenuId(resName: String?): Int {
    return requireActivity().getMenuId(resName)
}


/************************** 以下是View的资源扩展函数 ****************************/

fun View.getId(resName: String?): Int {
    return context.getId(resName)
}

fun View.getStringId(resName: String?): Int {
    return context.getStringId(resName)
}

fun View.getDrawableId(resName: String?): Int {
    return context.getDrawableId(resName)
}

fun View.getLayoutId(resName: String?): Int {
    return context.getLayoutId(resName)
}

fun View.getStyleId(resName: String?): Int {
    return context.getStyleId(resName)
}

fun View.getColorId(resName: String?): Int {
    return context.getColorId(resName)
}

fun View.getDimenId(resName: String?): Int {
    return context.getDimenId(resName)
}

fun View.getAnimId(resName: String?): Int {
    return context.getAnimId(resName)
}

fun View.getMenuId(resName: String?): Int {
    return context.getMenuId(resName)
}


/************************** 以下是Context的资源扩展函数 ****************************/
fun Context.getId(resName: String?): Int {
    return getResId(resName, RES_ID)
}

fun Context.getLayoutId(resName: String?): Int {
    return getResId(resName, RES_LAYOUT)
}

fun Context.getStringId(resName: String?): Int {
    return getResId(resName, RES_STRING)
}

fun Context.getDrawableId(resName: String?): Int {
    return getResId(resName, RES_DRAWABLE)
}

fun Context.getStyleId(resName: String?): Int {
    return getResId(resName, RES_STYLE)
}

fun Context.getColorId(resName: String?): Int {
    return getResId(resName, RES_COLOR)
}

fun Context.getDimenId(resName: String?): Int {
    return getResId(resName, RES_DIMEN)
}

fun Context.getAnimId(resName: String?): Int {
    return getResId(resName, RES_ANIM)
}

fun Context.getMenuId(resName: String?): Int {
    return getResId(resName, RES_MENU)
}

fun Context.getResId(
    resName: String?,
    defType: String?
): Int {
    return resources.getIdentifier(resName, defType, packageName)
}

@ColorInt
fun View.getCompatColor(@ColorRes id: Int): Int =
    context.getCompatColor(id)

@ColorInt
fun Context.getCompatColor(@ColorRes id: Int): Int =
    ResourcesCompat.getColor(resources, id, null)

@ColorInt
fun Fragment.getCompatColor(@ColorRes id: Int): Int =
    ResourcesCompat.getColor(resources, id, null)

fun View.getCompatDrawable(@DrawableRes id: Int): Drawable? =
    context.getCompatDrawable(id)

fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? =
    ResourcesCompat.getDrawable(resources, id, null)

fun Fragment.getCompatDrawable(@DrawableRes id: Int): Drawable? =
    ResourcesCompat.getDrawable(resources, id, null)

fun View.getCompatDimen(@DimenRes id: Int): Float =
    context.getCompatDimen(id)

fun Context.getCompatDimen(@DimenRes id: Int): Float =
    ResourcesCompat.getFloat(resources, id)

fun Fragment.getCompatDimen(@DimenRes id: Int): Float =
    ResourcesCompat.getFloat(resources, id)

fun View.getCompatFont(@FontRes id: Int): Typeface? =
    context.getCompatFont(id)

fun Context.getCompatFont(@FontRes id: Int): Typeface? =
    ResourcesCompat.getFont(this, id)

fun Fragment.getCompatFont(@FontRes id: Int): Typeface? =
    ResourcesCompat.getFont(requireActivity(), id)

fun View.getString(@StringRes id: Int): String =
    context.getString(id)

@ColorInt
fun parseColor(colorString: String): Int =
    Color.parseColor(colorString)


